<?php

function getHtml_by_parse_link($url) {
	$chtml = curl_init();
	curl_setopt($chtml, CURLOPT_URL, $url);
	curl_setopt($chtml, CURLOPT_FRESH_CONNECT, 1);
	curl_setopt($chtml, CURLOPT_POST, 0);
	curl_setopt($chtml, CURLOPT_RETURNTRANSFER, 1);
	$html = curl_exec($chtml);
	return $html;
}

function getImage_source($url) {
	$c = curl_init();
	curl_setopt($c, CURLOPT_POST, 0);
	curl_setopt($c, CURLOPT_URL, $url);
	curl_setopt($c, CURLOPT_HEADER, 0);
	curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	$res = curl_exec($c);
	curl_close($c);
	return $res;
}

function createImageLocation($mangaSlug, $chapterId) {
	$path = MANGA_DIR . $mangaSlug;
	if (!file_exists($path))
		mkdir($path, 0755);

	$path .= '/' . $chapterId;
	if (!file_exists($path))
		mkdir($path, 0755);
	return $path;
}

/**
 * Model to resize images to
 * 
 * @param type $full_path: full path to image file 
 * @param type $w: width to resize
 * @param type $h : height to resize
 */
function resizePic($src, $path, $w = 0, $h = 0) {
	file_put_contents($path, $src);
	if ($w == 0 && $h == 0) {
		$size = getimagesize($path);
		$w = $size[0];
		$h = $size[1];
	}
	$w = $w > MANGA_WIDTH ? MANGA_WIDTH : $w;
	$h = $w > MANGA_WIDTH ? ($h * MANGA_WIDTH / $w) : $h;

	$rz['image_library'] = 'gd2';
	$rz['source_image'] = $path;
	$rz['quality'] = MANGA_QUALITY;
	$rz['width'] = $w;
	$rz['height'] = $h;
	$CI = & get_instance();
	$CI->image_lib->initialize($rz);
	$CI->image_lib->resize();
	$CI->image_lib->clear();
}

?>
